package com.masterclient.app.modules.aftersale.adapter;

import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.bean.aftersale.AfterSalePurchaseItemBean;
import com.eoner.baselibrary.bean.aftersale.ProductPromotionItemBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.R;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * 作者：sean
 * 创建时间：2017/8/26 10:46
 * 功能说明：
 */

public class AfterSalePurchaseListAdapter extends BaseQuickAdapter<AfterSalePurchaseItemBean, BaseViewHolder> {


    public AfterSalePurchaseListAdapter() {
        super(R.layout.item_aftersale_order);
    }

    @Override
    protected void convert(BaseViewHolder helper, AfterSalePurchaseItemBean item) {

        helper.setTextColor(R.id.tv_status, Color.parseColor("#FF6809"))
                .setText(R.id.tv_status, item.getStatus_label());
        LinearLayout ll_shop = helper.getView(R.id.ll_shop);

        ll_shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(AroutePath.STORE_ACTIVITY)
                        .withString("store_id", item.getSeller().getId())
                        .withString("is_dealer", item.getSeller().isIs_dealer()?"1":"0")
                        .withString("is_agent", item.getSeller().isIs_agent()?"1":"0")
                        .navigation();
            }
        });

        //保管提货标识
        ImageView ivStorageOrder = helper.getView(R.id.iv_storage_order);
        ivStorageOrder.setVisibility("4".equals(item.getRefund_mode())
                ||"5".equals(item.getRefund_mode())?View.VISIBLE:View.GONE);
        ivStorageOrder.setImageResource("4".equals(item.getRefund_mode())
                ?R.mipmap.icon_storage_order:R.mipmap.icon_pick_order);

        helper.getView(R.id.tv_contact_seller).setVisibility(View.VISIBLE);
        helper.setGone(R.id.tv_detail, "1".equals(item.getButton().getView_btn()));

        TextView tv_shop_tag = helper.getView(R.id.tv_shop_tag);
        tv_shop_tag.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "fonts/iconfont.ttf"));
        TextView tv_shop_name = helper.getView(R.id.tv_shop_name);
        tv_shop_name.setText(item.getSeller().getStore_name());

        ((ImageView) helper.getView(R.id.tv_return)).setImageResource("2".equals(item.getRefund_type())
                ? R.mipmap.icon_bufa : "1".equals(item.getRefund_type()) ? R.mipmap.tuihuo1_shouhou : R.mipmap.tuikuan1_shouhou);
        helper.setText(R.id.tv_return_type, item.getRefund_type_label());
        LinearLayout llGood = helper.getView(R.id.ll_good);
        llGood.removeAllViews();
        if (item.getProducts() != null && item.getProducts().size() > 0) {
            for (int i = 0; i < item.getProducts().size(); i++) {

                ProductPromotionItemBean bean = item.getProducts().get(i);
                View view = View.inflate(mContext, R.layout.item_aftersale_goods, null);

                ViewTransformUtil.glideImageView(mContext, bean.getImage(),(ImageView) view.findViewById(R.id.iv_good),
                        new RoundedCornersTransformation(ScreenUtils.dp2px(4), 0), R.mipmap.defaults,
                        ScreenUtils.dp2px(107), ScreenUtils.dp2px(107));

                ((TextView) view.findViewById(R.id.tv_title)).setText(bean.getName());
                ((TextView) view.findViewById(R.id.tv_skus)).setText("规格：" + bean.getAttribute_desc());
                ((TextView) view.findViewById(R.id.tv_gos_num)).setText("x" + bean.getQty_ordered());
                //提货单
                TextView tvPickGoodNum = view.findViewById(R.id.tv_pick_good_num);
                LinearLayout layoutPrice = view.findViewById(R.id.layout_price);
                if ("5".equals(item.getRefund_mode())){
                    tvPickGoodNum.setText(new StringBuffer("提货数量 ").append(bean.getQty_ordered()));
                    tvPickGoodNum.setVisibility(View.VISIBLE);
                    layoutPrice.setVisibility(View.GONE);
                }else {
                    tvPickGoodNum.setVisibility(View.GONE);
                    layoutPrice.setVisibility(View.VISIBLE);
                }

                TextView returnMoney = ((TextView) view.findViewById(R.id.tv_return_money));
                TextView returnMoneyDesc = ((TextView) view.findViewById(R.id.tv_return_money_desc));
                LinearLayout returnMoneyLl = view.findViewById(R.id.ll_return_money);
                TextView returnIntegral = ((TextView) view.findViewById(R.id.tv_return_integral));
                LinearLayout returnIntegralLl = view.findViewById(R.id.ll_return_integral);
                Utils.setDin(returnMoney, mContext);
                Utils.setDin(returnIntegral, mContext);
                returnMoney.setTextColor(Color.parseColor("#FF5000"));
                returnMoneyDesc.setTextColor(Color.parseColor("#FF5000"));
                view.findViewById(R.id.view_line).setVisibility(i == item.getProducts().size() - 1 ? View.GONE : View.VISIBLE);

                if ("2".equals(item.getRefund_type())) {
                    returnIntegralLl.setVisibility(View.GONE);
                    returnMoneyLl.setVisibility(View.GONE);
                } else {
                    returnMoneyLl.setVisibility(!TextUtils.isEmpty(bean.getAmount()) && Float.parseFloat(bean.getAmount()) > 0 ? View.VISIBLE : View.GONE);
                    returnIntegralLl.setVisibility(!TextUtils.isEmpty(bean.getIntegral()) && Long.parseLong(bean.getIntegral()) > 0 ? View.VISIBLE : View.GONE);
                    returnIntegral.setText(!TextUtils.isEmpty(bean.getIntegral()) ? bean.getIntegral() : "0");
                    Utils.set_price_style2(returnMoney, !TextUtils.isEmpty(bean.getAmount()) ? ("¥ " + bean.getAmount()) : "0.00");
                }
                View cross_border_layout = view.findViewById(R.id.cross_border_layout);
                cross_border_layout.setVisibility(View.GONE);

                llGood.addView(view);

            }


        }
        helper.addOnClickListener(R.id.tv_contact_seller);


    }
}
